define(function(require, exports){
	var $		= require('jquery');
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	var STATUS = pubjs.config('CONST/OFFLINE_STATUS');
	var TYPE = {
		COMMON: 1, SCHEDULE: 3
	};
	// 自定义报表
	// 对应的接口还没有弄
	var CustomReport = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'P-reportList',
				urlRemove: 'customDownload/remove'
				,navEdit: 'report/custom/edit'
				,navSaveAs: 'report/custom/saveAs'
				,urlReset: 'customDownload/reset'
			});

			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			this.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('')+'</span>',
					'<span class="ml10" pub-name="add" pub-mod="@base/common/drop.add"/>',
				'</div>',
				'<div class="" pub-name="grid" pub-mod="report/grid/report.customReport"/>'
			].join('')));


			this.buildFromTemplate({
				add: {
					'def': LANG('新建报表'),
					'href': '#report/custom/edit',
					'href_target': '_self'
				},
				grid: {
					'hasFilter': true
				}
			});

			self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'height': 350,
				'items': [
					{'name': 'Status', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('状态：'),
						'data': [
							{id: 0, name: LANG('全部')},
							{id: STATUS.DONE, name: LANG('已完成')},
							{id: STATUS.FAIL, name: LANG('失败')},
							{id: STATUS.ING, name: LANG('进行中')}
						],
						'value': 0
					}},
					{'name': 'Type', 'uri': 'modules/sideBarBase.radio', 'config': {
						'title': LANG('类型：'),
						'data': [
							{id: 0, name: LANG('全部')},
							{id: TYPE.COMMON, name: LANG('普通报表')},
							{id: TYPE.SCHEDULE, name: LANG('定时报表')}
						],
						'value': 0
					}}
				]
			});

		}
		,onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {
					case 'Status':
						param.Status = +id;
						break;
					case 'Type':
						param.Type = +id;
						break;
					case 'Advertiser':
						param.Advertiser = id.length ? JSON.parse(id) : [];
						break;
				}
			});

			self.$.grid.setParam(param).load();
		}
		,onInputClick: function(ev) {
			return false;
		}
		,reload: function() {
			this.$.grid.load();
		}
		,reset: function() {
		}
		,onOperateMenuShow: function(ev){
			var param = ev.param;
			var result = [];
			// 检查是否有编辑权限
			if(param.Status === STATUS.PAUSE && pubjs.checkRight('customDownload.start'))
			{
				result.push({id: 'start', name: LANG('开始')});
			}

			if([STATUS.WAIT, STATUS.ING].indexOf(param.Status) > -1 && pubjs.checkRight('customDownload.pause'))
			{
				result.push({id: 'pause', name: LANG('终止')});
			}

			if(pubjs.checkRight('&customDownload.update,customDownload.info'))
			{
				result.push({id: 'edit', name: LANG('编辑')});
				result.push({id: 'saveAs', name: LANG('另存为')});
			}

			if([STATUS.DONE, STATUS.FAIL].indexOf(param.Status) > -1 && param.Type === TYPE.COMMON)
			{
				result.push({id: 'reset', name: LANG('重新生成')});
			}

			if(param.Status === STATUS.DONE && pubjs.checkRight('customDownload.download'))
			{
				result.push({id: 'download', name: LANG('下载')});
			}

			if(!result.length)
			{
				result.push({id: '', name: '没有操作'});
			}

			ev.returnValue = result;
			return false;
		}
		// 操作菜单-响应菜单操作
		,onOperateMenuSelect: function(ev){
			var op = ev.param[0];
			var data = ev.param[1];
			var c = this.getConfig();
			var id = +data._id;
			var self = this;

			var url = '';
			var param = {
				_id: id
			};

			switch(op.key){
				case 'download':
					return window.open('/api/dsp/download/offline/' + id);
					break;
				case 'reset':
					url = c.urlReset;
					break;
				case 'remove':
					url = c.urlRemove;
					break;
				case 'edit':
					return pubjs.controller.navigate(c.navEdit + '/' + id);
				case 'saveAs':
					return pubjs.controller.navigate(c.navSaveAs + '/' + id);
				default :
					return;
			}


			pubjs.loading.show();
			pubjs.mc.send(url, param, function(err, data) {
				pubjs.loading.hide();
				if(err)
				{
					return pubjs.alert(err.message);
				}

				pubjs.alert('操作成功');
				self.$.grid.reload();
			});

			return false;
		}
		// 批量操作-下拉菜单界面
		,onBatchShow: function(ev){
			var list = [];
			if (pubjs.checkRight('customDownload.download'))
			{
				list = [
					{id: 'download', name: LANG('下载')}
				];
			}
			else
			{
				list = [
					{id: '', name: LANG('没有操作')}
				];
			}

			ev.returnValue = list;
			return false;
		}
		// 批量操作-响应菜单操作
		,onBatchSelect: function(ev){
			var param = ev.param[0];
			var data = ev.param[2];
			var key = param.key || '';
			var ids = [];
			for(var i = 0; i < data.length; i++)
			{
				if(data[i].Status === STATUS.DONE)
				{
					ids.push(data[i]._id);
				}
			}

			if(!ids || !ids.length)
			{
				pubjs.alert(LANG('请先选择可以下载的任务'));
				return false;
			}

			switch(key)
			{
				case 'download':
					for(i = 0; i < ids.length; i++)
					{
						window.open('/api/dsp/download/offline/' + ids[i]);
					}
					break;
			}

			this.$.grid.setSelectRowIds([]);
			this.reload();
			return false;
		}
		,onShowSchedule: function(ev){
			var self = this;
			var mod = self.$.schedulePop;
			if(!mod)
			{
				this.createAsync('schedulePop',"report/popwin/popwin.base", {
				},function(mod) {
					mod.setParam(ev.param).reset().show();
				})
			}
			else
			{
				mod.setParam(ev.param).reset().show();
			}
		}
	});
	exports.customReport = CustomReport;

});